package com.jrx.anytxn.param.job.partner;


import com.jrx.anytxn.param.config.AnytxnBatchProperties;
import com.jrx.anytxn.param.constant.Constant;
import com.jrx.anytxn.param.entity.PmPartnerInfo;
import com.jrx.anytxn.param.mapper.ext.ExtPmPartnerInfoMapper;
import com.jrx.anytxn.param.service.partner.IPartnerGeneralService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepScope;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;
import java.util.List;

/**
 * 日终更新合作方当日限额
 */
@Configuration
@EnableConfigurationProperties(AnytxnBatchProperties.class)
public class UpdatePartnerLimitConfiguration {

    private static final Logger logger = LoggerFactory.getLogger(UpdatePartnerLimitConfiguration.class);

    @Autowired
    public JobBuilderFactory jobBuilderFactory;
    @Autowired
    public StepBuilderFactory stepBuilderFactory;
    @Resource
    private ExtPmPartnerInfoMapper extPmPartnerInfoMapper;
    @Autowired
    private IPartnerGeneralService partnerGeneralService;


    @Bean
    public Job updatePartnerLimitJob() {
        return jobBuilderFactory.get("updatePartnerLimitJob")
                .start(updatePartnerLimitStep())
                .build();
    }

    @Bean
    public Step updatePartnerLimitStep() {
        return stepBuilderFactory.get("updatePartnerLimitStep")
                .tasklet(updatePartnerLimitTasklet())
                .build();
    }

    @Bean
    @StepScope
    public Tasklet updatePartnerLimitTasklet() {
        return (stepContribution, chunkContext) -> {
            List<PmPartnerInfo> pmPartnerInfoList = extPmPartnerInfoMapper.findByOrgIdForBatch(Constant.DEFAULT_ORG_NUMBER);
            partnerGeneralService.updatePartnerLimit(pmPartnerInfoList);

            logger.info("合作方当日额度更新完成");
            return RepeatStatus.FINISHED;
        };
    }
}
